/**
 * Created by Administrator on 2018/1/27.
 */
SESSION = {};
SESSION.community_id = 0;
SESSION.community_name = "";
SESSION.location_store = false;
SESSION.get_community_id = function () {
    if (SESSION.community_id) {
        return SESSION.community_id;
    } else {
        SESSION.community_id = Storage_.get("community_id");
        if (SESSION.community_id) {
            return SESSION.community_id;
        }
    }
	my_tips_("请选择小区",function(){
	    go_("choice_community");
	});
    go_("choice_community");
}

SESSION.get_community_name = function () {
    if (SESSION.community_name) {
        return SESSION.community_name;
    } else {
        SESSION.community_name = Storage_.get("community_name");
        if (SESSION.community_name) {
            return SESSION.community_name;
        }
    }
    my_tips_("请选择小区",function(){
    	go_("choice_community");
    });
    go_("choice_community");
}

SESSION.set_community_name = function (community_name) {
    SESSION.community_name = community_name;
    Storage_.set("community_name", community_name);
    return SESSION.community_name;
}
SESSION.set_community_id = function (community_id) {
    SESSION.community_id = community_id;
    Storage_.set("community_id", community_id);
    return SESSION.community_id;
}

/**
 * 定位获取位置信息
 * @param param
 * @param callback
 */
SESSION.location = function (callback) {
    if(typeof callback != "function"){
        console.error("callback is not function ")
        return;
    }
    if (SESSION.location_store) {
        callback(SESSION.location_store);
    }else{
        SESSION.location_store = Storage_.get("location_store");
        if(SESSION.location_store){
            callback(SESSION.location_store);
        }else{
            var geolocation = new BMap.Geolocation();
            geolocation.getCurrentPosition(function (r) {
                if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                    var param = {latitude: r.point.lat, longitude: r.point.lng};
                    // 请求获取
                    var url = "http://api.map.baidu.com/geocoder/v2/";
                    var data = {
                        location: param.latitude + ',' + param.longitude,
                        output: 'json',
                        ak: 'caCnpncxlSUt8XhZ8RaWT2gQO1aSaBhT'
                    }
                    $.ajax({
                        async: true,
                        url: url,
                        data: data,
                        type: 'GET',
                        dataType: "jsonp", // 返回的数据类型，设置为JSONP方式
                        success: function (r_data) {
                            if(r_data.status == 0){
                                param.location = r_data;
                                param.ad_code = r_data.result.addressComponent.adcode;
                                param.formatted_address = r_data.result.formatted_address;
                                param.province = r_data.result.addressComponent.province;
                                param.city = r_data.result.addressComponent.city;
                                param.district = r_data.result.addressComponent.district;
                                Storage_.set("location_store", param);
                                callback(param);
                            }else{
                                my_tips_("定位失败！稍后再试。");
                            }
                        }
                    });
                    //结束请求
                }
            });

        }
    }

}